home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CHARTP10.ARJ / RULES.CPP < prev    next >
C/C++ Source or Header  |  1992-01-26  |  1KB  |  56 lines

  1.  
  2. // Copyright 1992, David Perelman-Hall & Jamshid Afshar
  3.  
  4. #include <fstream.h>
  5. #include <strstream.h>
  6. #include <string.h>
  7. #include "rules.h"
  8.  
  9.  
  10. ostream& operator << ( ostream& os, const Rule& rule )
  11. {
  12.    os << rule._lhs << " -> " << rule._rhs;
  13.    return os;
  14. }
  15.  
  16. RuleList::RuleList()
  17.    : _num(0)
  18. {
  19. }
  20.  
  21. const MAX_LINE = 260;
  22. void RuleList::read(const char *file_name)
  23. {
  24.    ifstream infile(file_name);
  25.    char buff[MAX_LINE];
  26.  
  27.    while (infile.good()) {
  28.       infile.getline(buff, MAX_LINE);
  29.       if (infile.eof()) break;
  30.       add(buff);
  31.    }
  32. }
  33.  
  34. void RuleList::add(const char *rule_str)
  35. {
  36.    assert(_num < MAX_RULES);
  37.  
  38.    char temp_rule_str[MAX_LINE];
  39.    strcpy(temp_rule_str, rule_str);
  40.    istrstream rule_stream(temp_rule_str, strlen(rule_str)+1);
  41.  
  42.    char cat_str[MAX_STRING_LEN];
  43.    rule_stream >> cat_str;
  44.    _arr[_num]._lhs = Category(cat_str);
  45.  
  46.    _arr[_num]._rhs.clear();
  47.    while (rule_stream.good()) {
  48.       rule_stream >> cat_str;
  49.       _arr[_num]._rhs += Category(cat_str);
  50.    }
  51.  
  52.    cout << "Adding rule: " << _arr[_num] << "\n";
  53.  
  54.    _num++;
  55. }
  56.